package fileStudy;

import java.io.*;

public class ReadTxt {

    public static void read() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("F:\\IDEACodeOFJavaEE\\src\\Test1.java");
            //定义数据缓冲区，(临时存放数据的区域)
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fis.read(buffer)) != -1){
                String str = new String(buffer,0,len);
                System.out.println(str);
                System.out.println("len="+len+"个字节。");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ex){
            ex.printStackTrace();
        }

        finally {
            if (fis != null) {
                try {
                    fis.close();
                    fis = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void writeTxt(){
        String contern = "我是花甲的干爹，哼。\r\n我是世界上最骚的男人！";

        FileOutputStream fos = null;

        try {
            //创建文件输入流对象
            fos = new FileOutputStream("D:\\backup\\我的.txt");
           byte[] date=  contern.getBytes("UTF-8");
            fos.write(date);
            fos.flush();//清空缓冲区(存储数据的临时区域)；
            System.out.println("保存文件成功！");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != fos ){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }






    public static void main(String[] args) {
        writeTxt();
        read();
    }
}
